package cn.zucc.edu.bruce.iot;

public class DataDeal {
    public static String bytesToString(byte[] bytes, int len) {
        char[] chars = new char[len];
        for (int i = 0; i < len; i++)
            chars[i] = (char) (bytes[i] & 0xff);
        return new String(chars);
    }

    public static byte[] stringToBytes(String s) {
        return s.getBytes();
    }

    public static short bytesToShort(byte[] bytes) {
        int b0 = bytes[0] & 0xFF;
        int b1 = bytes[1] & 0xFF;
        return (short) ((b1 << 8) | b0);
    }

    public static byte[] shortToBytes(short s) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (s >> 8 & 0xFF);
        bytes[1] = (byte) (s & 0xFf);
        return bytes;
    }

    public static char byteToChar(byte b) {
        return (char) (b & 0xFF);
    }

    public static byte charToByte(char c) {
        return (byte) (c & 0xFF);
    }

    public static int bytesToInt(byte[] bytes, int offset) {
        int b0 = bytes[offset] & 0xFF;
        int b1 = bytes[1 + offset] & 0xFF;
        int b2 = bytes[2 + offset] & 0xFF;
        int b3 = bytes[3 + offset] & 0xFF;
        return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
    }

    public static byte[] intToBytes(int number) {
        byte[] bytes = new byte[4];
        bytes[3] = (byte) (number >> 24 & 0xFF);
        bytes[2] = (byte) (number >> 16 & 0xFF);
        bytes[1] = (byte) (number >> 8 & 0xFF);
        bytes[0] = (byte) (number & 0xFF);
        return bytes;
    }

    public static byte setCheckSum(byte[] bytes) {
        int item = 0;
        for (int i = 0; i < 51; i++)
            item += bytes[i] & 0xff;
        return (byte) (item & 0xff);
    }
    public static byte Check(byte[] bytes){
        int item = 0;
        for (int i = 0; i < 71; i++)
            item += bytes[i] & 0xff;
        return (byte) (item & 0xff);
    }
    public static void main(String[] args) {
        System.out.println(stringToBytes("317024").length);
    }
}
